﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;

//namespace TLD_Vision.filters {
//    public static class LineDetection {
//        private static float[][] PrewittXMask = {
//            new float[] {-1f, 0f, 1f},
//            new float[] {-1f, 0f, 1f},
//            new float[] {-1f, 0f, 1f}};

//        private static float[][] PrewittYMask = {
//            new float[] {-1f, -1f, -1f},
//            new float[] {0f, 0f, 0f},
//            new float[] {1f, 1f, 1f}};

//        private static float[][] OmniDirectionalLineDetector = {
//                                                                   new float[] {-1, -1, -1},
//                                                                   new float[] {-1, 8, -1},
//                                                                   new float[] {-1, -1, -1}};

//        public static Image ApplyPrewitt(this Image img) {
//            ImageData data1 = img.ApplyUnnormalizedMask(PrewittXMask);
//            ImageData data2 = img.ApplyUnnormalizedMask(PrewittYMask);
//            ImageData combined = data1.Combine(data2);
//            combined.RemapToARGB();

//            return combined.ToImage();
//        }

//        private static ImageData ApplyPrewittAndNMS(ImageData DerivativeX, ImageData DerivativeY, ImageData Gradient) {
//            int Width = DerivativeX.Width, Height = DerivativeY.Height;
//            ImageData NonMax = new ImageData(Width, Height);

//            for (int i = 0; i <= (Height - 1); i++) {
//                for (int j = 0; j <= (Width - 1); j++) {
//                    NonMax[i * Width + j] = Gradient[i * Width + j];
//                }
//            }

//            int Limit = 3 / 2;
//            float Tangent;

//            for (int i = Limit; i <= (Height - Limit) - 1; i++) {
//                for (int j = Limit; j <= (Width - Limit) - 1; j++) {

//                    if (DerivativeX[(i) * Width + (j)] == 0)
//                        Tangent = 90F;
//                    else
//                        Tangent = (float)(Math.Atan(DerivativeY[(i) * Width + (j)] / DerivativeX[(i) * Width + (j)]) * 180 / Math.PI); //rad to degree



//                    //Horizontal Edge
//                    if (((-22.5 < Tangent) && (Tangent <= 22.5)) || ((157.5 < Tangent) && (Tangent <= -157.5))) {
//                        if ((Gradient[(i) * Width + (j)] < Gradient[(i) * Width + (j + 1)]) || (Gradient[(i) * Width + (j)] < Gradient[(i) * Width + (j - 1)]))
//                            NonMax[(i) * Width + (j)] = 0;
//                    }


//                    //Vertical Edge
//                    if (((-112.5 < Tangent) && (Tangent <= -67.5)) || ((67.5 < Tangent) && (Tangent <= 112.5))) {
//                        if ((Gradient[(i) * Width + (j)] < Gradient[(i + 1) * Width + (j)]) || (Gradient[(i) * Width + (j)] < Gradient[(i - 1) * Width + (j)]))
//                            NonMax[(i) * Width + (j)] = 0;
//                    }

//                    //+45 Degree Edge
//                    if (((-67.5 < Tangent) && (Tangent <= -22.5)) || ((112.5 < Tangent) && (Tangent <= 157.5))) {
//                        if ((Gradient[(i) * Width + (j)] < Gradient[(i + 1) * Width + (j - 1)]) || (Gradient[(i) * Width + (j)] < Gradient[(i - 1) * Width + (j + 1)]))
//                            NonMax[(i) * Width + (j)] = 0;
//                    }

//                    //-45 Degree Edge
//                    if (((-157.5 < Tangent) && (Tangent <= -112.5)) || ((67.5 < Tangent) && (Tangent <= 22.5))) {
//                        if ((Gradient[(i) * Width + (j)] < Gradient[(i + 1) * Width + (j + 1)]) || (Gradient[(i) * Width + (j)] < Gradient[(i - 1) * Width + (j - 1)]))
//                            NonMax[(i) * Width + (j)] = 0;
//                    }

//                }
//            }

//            return NonMax;
//        }

//        public static Image ApplyPrewittAndNMS(this Image img) {
//            ImageData DerivativeX = img.ApplyUnnormalizedMask(PrewittXMask);
//            ImageData DerivativeY = img.ApplyUnnormalizedMask(PrewittYMask);
//            ImageData Gradient = DerivativeX.Combine(DerivativeY);
//            ImageData output = ApplyPrewittAndNMS(DerivativeX, DerivativeY, Gradient);
//            output.RemapToARGB();

//            return output.ToImage();
//        }

//        public static Image DetectPlate(this Image img) {
//            ImageData DerivativeX = img.ApplyUnnormalizedMask(PrewittXMask);
//            ImageData DerivativeY = img.ApplyUnnormalizedMask(PrewittYMask);
//            ImageData Gradient = DerivativeX.Combine(DerivativeY);
//            ImageData output = new ImageData(DerivativeX.Width, DerivativeX.Height);
//            int height = DerivativeX.Height, width = DerivativeX.Width;

//            for (int y = 0; y < height; ++y) {
//                for (int x = 0; x < width; ++x) {
//                    int index = y * height + x;
//                    if (Gradient[index] > 0) {
//                        output[index] = -1;
//                    }
//                }
//            }

//            return output.ToImage();
//        }

//        public static Image HoughTransform(this Image img) {
//            ImageData DerivativeX = img.ApplyUnnormalizedMask(PrewittXMask);
//            ImageData DerivativeY = img.ApplyUnnormalizedMask(PrewittYMask);
//            ImageData Gradient = DerivativeX.Combine(DerivativeY);
//            ImageData output = ApplyPrewittAndNMS(DerivativeX, DerivativeY, Gradient);
//            output.RemapToARGB();

//            int width = output.Width, height = output.Height;

//            int rMax = (int)Math.Sqrt(Math.Pow(width, 2) + Math.Pow(height, 2));
//            ImageData Accumulator = new ImageData(2 * rMax, 180);

//            int resolution = 1;

//            double[] cos = new double[180 / resolution];
//            double[] sin = new double[180 / resolution];
//            for (int theta = 0; theta < 180; theta += resolution) {
//                cos[theta] = Math.Cos(theta);
//                sin[theta] = Math.Sin(theta);
//            }

//            for (int y = 0; y < height; ++y) {
//                for (int x = 0; x < width; ++x) {
//                    for (int theta = 0; theta < 180; theta += resolution) {
//                        if ((output[y * width + x] & 0xFF) > 30) {
//                            int rho = (int)(x * cos[theta] + y * sin[theta]);
//                            Accumulator[(rMax + rho) * 180 + theta]++;
//                        }
//                    }
//                }
//            }

//            Accumulator.RemapToARGB();
//            return Accumulator.ToImage();
//        }
//    }
//}
